#pragma once

#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include "websocketpp/config/asio_no_tls_client.hpp"

namespace p2pms
{

//==================================================================================================
// 
//==================================================================================================
class IWebSocketHandler
{
public:
    virtual void OnConnected() = 0;
    virtual void OnMessage(const std::string& msg) = 0;
    virtual void OnDisconnected() = 0;
};

class WebSocketClient
{
public:
    using WsClient = websocketpp::client<websocketpp::config::asio_client>;

    WebSocketClient() = default;
    ~WebSocketClient();

	bool Init(IWebSocketHandler* handler);
    bool Start(const std::string& uri);
    void Stop();
	bool SendMsg(const std::string& msg);

private:
    void OnOpen(websocketpp::connection_hdl hdl);
    void OnMessage(websocketpp::connection_hdl hdl, WsClient::message_ptr msg);
    void OnClose(websocketpp::connection_hdl hdl);

private:
    WsClient m_client;
    IWebSocketHandler* m_handler = nullptr;
	std::string m_uri;
    asio::io_context m_ioc;
    std::thread m_io_thread;
    websocketpp::connection_hdl m_conn_hdl;
    std::mutex m_hdl_mutex;
};

}